package com.rpc.netty.simpleNetty;

import io.netty.bootstrap.Bootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;

/**
 * @author 何章怀晓 hezhanghuaixiao
 * @version V1.0
 * @desc
 */
public class NettyClient {

  public static void main(String[] args) throws InterruptedException {
    EventLoopGroup eventLoopGroup = new NioEventLoopGroup();

    try {

      Bootstrap bootstrap = new Bootstrap();

      bootstrap.group(eventLoopGroup)//线程组
          .channel(NioSocketChannel.class)//客户端通道实现类
          .handler(new ChannelInitializer<SocketChannel>() {
            @Override
            protected void initChannel(SocketChannel ch) throws Exception {
              ch.pipeline().addLast(new NettyClientHandler());
            }
          });

      System.out.println("客户端Ok...");

      ChannelFuture channelFuture = bootstrap.connect("localhost", 8888).sync();

      channelFuture.channel().closeFuture().sync();


    } catch (Exception e) {
      eventLoopGroup.shutdownGracefully();
    }
  }

}
